<template>
  <div class="story-generator">
    <h2>故事情节生成</h2>

    <div class="input-section">
      <label for="story-prompt">故事主题或关键词:</label>
      <input
        id="story-prompt"
        v-model="prompt"
        type="text"
        placeholder="例如: 未来世界的机器人侦探">

      <div class="length-selector">
        <label>故事长度:</label>
        <div class="radio-group">
          <label v-for="option in lengthOptions" :key="option.value">
            <input
              type="radio"
              v-model="length"
              :value="option.value">
            {{ option.label }}
          </label>
        </div>
      </div>

      <button @click="generateStory" :disabled="isGenerating" class="generate-btn">
        {{ isGenerating ? '生成中...' : '生成故事' }}
      </button>
    </div>

    <div v-if="generatedStory" class="result-section">
      <h3>生成的故事剧情</h3>
      <div class="story-content">{{ generatedStory }}</div>

      <div class="actions">
        <button @click="copyToClipboard" class="copy-btn">复制文本</button>
        <button @click="useForComic" class="use-btn">用于漫画生成</button>
      </div>
    </div>

    <div v-if="isGenerating" class="loading">
      <div class="spinner"></div>
      <p>正在创作精彩故事...</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      prompt: "",
      length: "medium",
      lengthOptions: [
        { value: "short", label: "短篇" },
        { value: "medium", label: "中篇" },
        { value: "long", label: "长篇" }
      ],
      isGenerating: false,
      generatedStory: ""
    };
  },
  methods: {
    async generateStory() {
      if (!this.prompt.trim()) {
        alert("请输入故事主题");
        return;
      }

      this.isGenerating = true;
      this.generatedStory = "";

      try {
        const response = await this.$http.post("/api/generate-story", {
          prompt: this.prompt,
          length: this.length
        });

        this.generatedStory = response.data.story;
      } catch (error) {
        console.error("生成故事失败:", error);
        alert("故事生成失败，请重试");
      } finally {
        this.isGenerating = false;
      }
    },
    copyToClipboard() {
      navigator.clipboard.writeText(this.generatedStory)
        .then(() => alert("已复制到剪贴板"))
        .catch(err => console.error("复制失败:", err));
    },
    useForComic() {
      this.$emit("story-generated", this.generatedStory);
      alert("故事已准备好用于漫画生成，请切换到文本转漫画模块");
    }
  }
};
</script>

<style scoped>
.story-generator {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.input-section {
  background-color: #f9f9f9;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.input-section label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
}

.input-section input[type="text"] {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-bottom: 15px;
  font-size: 1em;
}

.length-selector {
  margin-bottom: 15px;
}

.radio-group {
  display: flex;
  gap: 15px;
  margin-top: 5px;
}

.radio-group label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-weight: normal;
  cursor: pointer;
}

.generate-btn {
  background-color: #4CAF50;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  font-size: 1em;
  cursor: pointer;
}

.generate-btn:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.result-section {
  background-color: #f5f5f5;
  padding: 20px;
  border-radius: 8px;
  margin-top: 20px;
}

.story-content {
  white-space: pre-wrap;
  line-height: 1.6;
  margin: 15px 0;
}

.actions {
  display: flex;
  gap: 10px;
  margin-top: 15px;
}

.copy-btn, .use-btn {
  padding: 8px 15px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.copy-btn {
  background-color: #2196F3;
  color: white;
}

.use-btn {
  background-color: #FF9800;
  color: white;
}

.loading {
  text-align: center;
  margin: 30px 0;
}

.spinner {
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  border-top: 4px solid #4CAF50;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
  margin: 0 auto 15px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>